﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DispatchAssist.Weather.Interfaces;

namespace DispatchAssist.Weather.Implements
{
    class WNCityRadarWeatherMapInfo : IGetPics
    {
        public DateTime Date { get; private set; }

        public IWeatherMapInfo WeatherMapInfo { get; private set; }

        public const int DisplayInterval = 15;//每15分钟

        public const int MapInterval = 6;//每6分种一张图

        public WNCityRadarWeatherMapInfo(IWeatherMapInfo mapInfo)
        {
            WeatherMapInfo = mapInfo;

            Date = DateTime.Now.AddMinutes(-DisplayInterval);
        }

        public void SetPreviousDateTime()
        {
            Date = Date.AddMinutes(-DisplayInterval);
        }

        public string GetNewestPic()
        {
            var str = GetPic(Date);
            CarolLib.Log.Instance.Info("城市最新图片地址："+str);
            return str;
        }

        private string GetPic(DateTime date)
        {
            //http://i.weather.com.cn/i/product/pic/l/sevp_aoc_rdcp_sldas_ebref_az9210_l88_pi_20110905051000000.gif
            return string.Format(@"http://i.weather.com.cn/i/product/pic/l/sevp_aoc_rdcp_sldas_ebref_{0}_l88_pi_{1}{2}.GIF",
                    WeatherMapInfo.InfoID,  GetDate2(date), GetDate3(date));
        }

        private string GetDate2(DateTime date)
        {
            if (date.Hour < 8)
            {
                date = date.AddDays(-1);
            }
            return date.ToString("yyyyMMdd");
        }
        private string GetDate3(DateTime date)
        {
            //从8点开始，显示半个小时前的图片
            //前一天的160000-今天的155000为当天时间
            //000000	08:00:00
            //001000	08:10:00
            //002000	08:20:00
            //003000	08:30:00
            //010000	09:00:00
            //020000	10:00:00
            //030000	11:00:00
            //050000	13:00:00

            return string.Format("{0:00}{1:00}00000", date.Hour >= 8 ? (date.Hour - 8) : (date.Hour + 16), (date.Minute / MapInterval) * 5);

        }

        public IList<string> GetAnimatePics(double time)
        {
            IList<string> pics = new List<string>();

            var beginDate = Date.AddHours(-time);

            CarolLib.Log.Instance.Info(string.Format("城市天气动画图片地址开始,图片开始时间{0}", beginDate.ToString("yyyyMMdd HH:mm:ss")));
           
            for (DateTime startDate = beginDate; startDate <= Date; startDate = startDate.AddMinutes(MapInterval))
            {
                var str = GetPic(startDate);
                CarolLib.Log.Instance.Info("城市天气动画图片地址：" + str);
                pics.Add(str);
            }
            return pics;
        }
    }
}
